package gamecode;

import java.io.IOException;

import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;

public class DeltaMain extends BasicGame {
	
	// game version
	public final static double version = 0.1;
	
	// game variables
	final int menuSpacing = 15;
	boolean inMenu = false;
	int menuItem = 0;
	boolean inGame = false;
	static SavedState settings;
	
	// dynamic variables
	EntityManager emanager;
	SpaceBackground spacebg;
	EntityPlayerShip playership;

	public DeltaMain() {
		super("Delta - v" + version);
	}
	
	public static void main(String[] args) throws SlickException {
		AppGameContainer app = new AppGameContainer(new DeltaMain());
		settings = new SavedState("data/game/settings.conf");
		int width = 800;
		int height = 600;
		try {
			settings.load();
			width = (int)settings.getNumber("window.Width");
			height = (int)settings.getNumber("window.Height");
			if(width == 0) width = 800;
			if(height == 0) height = 600;
		}
		catch (Exception ex) {
			System.out.println("Error loading settings: " + ex.getMessage());
		}
		app.setDisplayMode(width, height, false);
		app.start();
	}
	
	public void init(GameContainer gc) throws SlickException {
		initNewGame(gc);
		displayMenu();
	}
	
	public void update(GameContainer gc, int delta) throws SlickException {
		Input input = gc.getInput();
		
		if(!inMenu) {
			if(input.isKeyPressed(Input.KEY_ESCAPE)) {
				while(input.isKeyPressed(Input.KEY_ESCAPE));
				displayMenu();
			}
			if(input.isMousePressed(0)) {
				emanager.addEntity(new EntityDebugBouy(Color.yellow, new DPoint(input.getAbsoluteMouseX() + (playership.getPosition().getX() - gc.getWidth()/2), input.getAbsoluteMouseY() + (playership.getPosition().getY() - gc.getHeight()/2))));
			}
			
			playership.checkInput(input, delta);
			playership.update(1);
		}
		else {
			if(input.isKeyPressed(Input.KEY_DOWN) || input.isKeyPressed(Input.KEY_S)) {
				menuItem += 1;
				if(inGame) {
					if(menuItem > 2) menuItem = 0;
				}
				else {
					if(menuItem > 1) menuItem = 0;
				}
			}
			if(input.isKeyPressed(Input.KEY_UP) || input.isKeyPressed(Input.KEY_W)) {
				menuItem -= 1;
				if(inGame) {
					if(menuItem < 0) menuItem = 2;
				}
				else {
					if(menuItem < 0) menuItem = 1;
				}
			}
			if(input.isKeyPressed(Input.KEY_ENTER)) {
				if(inGame) {
					if(menuItem == 0) {
						closeMenu();
					}
					else if(menuItem == 1) {
						closeMenu();
						initNewGame(gc);
						displayMenu();
					}
					else if(menuItem == 2) {
						exitDelta(gc);
					}
				}
				else {
					if(menuItem == 0) {
						closeMenu();
						initNewGame(gc);
						inGame = true;
					}
					else if(menuItem == 1) {
						exitDelta(gc);
					}
				}
			}
		}
	}
	
	public void render(GameContainer gc, Graphics g) throws SlickException {
		spacebg.drawStars(g, new Rectangle(0, 0, gc.getWidth(), gc.getHeight()), playership);
		if(inGame) {
			emanager.drawEntities(g, new Rectangle(0, 0, gc.getWidth(), gc.getHeight()), playership);
		}
		if(inMenu && !inGame) {
			g.setColor(Color.white);
			if(menuItem == 0) {
				g.drawString("> New game <", gc.getWidth()/2-(("> New game <").length()), gc.getHeight()/2 - menuSpacing);
				g.drawString("Exit Delta", gc.getWidth()/2-(("Exit Delta").length()), gc.getHeight()/2);
			}
			else if(menuItem == 1) {
				g.drawString("New game", gc.getWidth()/2-(("New game").length()), gc.getHeight()/2 - menuSpacing);
				g.drawString("> Exit Delta <", gc.getWidth()/2-(("> Exit Delta <").length()), gc.getHeight()/2);
			}
		}
		else if(inMenu && inGame) {
			g.setColor(Color.white);
			if(menuItem == 0) {
				g.drawString("> Continue game <", gc.getWidth()/2-(("> Continue game <").length()), gc.getHeight()/2 - menuSpacing);
				g.drawString("Return to main menu", gc.getWidth()/2-(("Return to main menu").length()), gc.getHeight()/2);
				g.drawString("Exit Delta", gc.getWidth()/2-(("Exit Delta").length()), gc.getHeight()/2 + menuSpacing);
			}
			else if(menuItem == 1) {
				g.drawString("Continue game", gc.getWidth()/2-(("Continue game").length()), gc.getHeight()/2 - menuSpacing);
				g.drawString("> Return to main menu <", gc.getWidth()/2-(("> Return to main menu <").length()), gc.getHeight()/2);
				g.drawString("Exit Delta", gc.getWidth()/2-(("Exit Delta").length()), gc.getHeight()/2 + menuSpacing);
			}
			else if(menuItem == 2) {
				g.drawString("Continue game", gc.getWidth()/2-(("Continue game").length()), gc.getHeight()/2 - menuSpacing);
				g.drawString("Return to main menu", gc.getWidth()/2-(("Return to main menu").length()), gc.getHeight()/2);
				g.drawString("> Exit Delta <", gc.getWidth()/2-(("> Exit Delta <").length()), gc.getHeight()/2 + menuSpacing);
			}
		}
	}
	
	public void displayMenu() {
		menuItem = 0;
		inMenu = true;
	}
	
	public void closeMenu() {
		menuItem = 0;
		inMenu = false;
	}
	
	public void initNewGame(GameContainer gc) throws SlickException {
		inGame = false;
		emanager = new EntityManager();
		playership = new EntityPlayerShip(new Image("data/player/ship.png"), Color.green);
		playership.setPosition(60, 60);
		emanager.addEntity(playership);
		emanager.addEntity(new EntityDebugBouy(Color.blue, new DPoint()));
		spacebg = new SpaceBackground(40, new Rectangle(0, 0, gc.getWidth(), gc.getHeight()), playership);
		gc.getGraphics().clearClip();
		gc.getGraphics().setClip(0, 0, gc.getWidth(), gc.getHeight());
	}
	
	public void exitDelta(GameContainer gc) {
		settings.setNumber("window.Width", gc.getWidth());
		settings.setNumber("window.Height", gc.getHeight());
		try {
			settings.save();
		} catch (Exception ex) {
			System.out.println("Error saving settings: " + ex.getMessage());
		}
		gc.exit();
	}
}
